﻿using AutoMapper;
using Inventory.API.Application.Command.WarehouseManagementCommand;
using Inventory.API.Utils;
using Inventory.Domain;
using Inventory.Errorcode;
using Inventory.Errorcode.ErrorCode;
using Inventory.Infrastructure.Interfaces;
using Inventory.Infrastructure.Repositories;
using MediatR;


namespace Inventory.API.Application.CommandHandler.WarehousemanagementCommandHandler
{
    public class GetWarehousingListQueryCommandHandler : IRequestHandler<GetWarehousingListQueryCommand, ApiResult<PagingResult<IList<Warehousemanagement>>>>
    {
        private readonly ILogger<GetWarehousingListQueryCommandHandler> _logger;
        private readonly IMapper _mapper;
        private readonly IWarehousemanagementRepository repository;

        public GetWarehousingListQueryCommandHandler(ILogger<GetWarehousingListQueryCommandHandler> logger, IMapper mapper, IWarehousemanagementRepository repository)
        {
            _logger = logger;
            _mapper = mapper;
            this.repository = repository;
        }

        async Task<ApiResult<PagingResult<IList<Warehousemanagement>>>> IRequestHandler<GetWarehousingListQueryCommand, ApiResult<PagingResult<IList<Warehousemanagement>>>>.Handle(GetWarehousingListQueryCommand request, CancellationToken cancellationToken)
        {
            try
            {

                var productionPlans = (await repository.GetAllAsync(x=>x.IsDeleted!=true)).AsQueryable();



                // 条件查询
                if (!string.IsNullOrEmpty(request.Warehousecode))
                {
                    productionPlans = productionPlans.Where(x => x.Warehousecode == request.Warehousecode);
                }

                if (!string.IsNullOrEmpty(request.Warehousename))
                {
                    productionPlans = productionPlans.Where(x => x.Warehousename == request.Warehousename);
                }
                if (!string.IsNullOrEmpty(request.Location))
                {
                    productionPlans = productionPlans.Where(x => x.Location == request.Location);
                }
                if (!string.IsNullOrEmpty(request.Director))
                {
                    productionPlans = productionPlans.Where(x => x.Director == request.Director);
                }

                //当前页的数据
                var currentData = _mapper.Map<IList<Warehousemanagement>>(productionPlans.Skip(request.PageSize * (request.PageIndex - 1)).Take(request.PageSize).ToList());
               
                //分页
                var pagingResult = new PagingResult<IList<Warehousemanagement>>
                {
                    TotalCount = productionPlans.Count(),
                    TotalPage = Math.Ceiling(productionPlans.Count() / (double)request.PageSize),
                    Data = currentData
                };



                // 返回成功结果
                return ApiResult<PagingResult<IList<Warehousemanagement>>>.Success(pagingResult, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error when getting warehousing list.");
                throw;
            }
        }
    }
}
